package cn.bluech.lib.ui.widget.datetimepicker

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.format.DateFormat
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import cn.bluech.lib.R
import cn.bluech.lib.utils.DispUtils
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.YEAR
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.MONTH
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.DAY
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.HOUR
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.MIN
import cn.bluech.lib.ui.widget.datetimepicker.DateTimePicker.Companion.SECOND
import com.google.android.material.bottomsheet.BottomSheetDialog


/**
 *
 */
open class DatePickerDialog(private val ctx: Context) {
    companion object {
        const val CARD = 0 //卡片
        const val CUBE = 1 //方形
        const val STACK = 2 //顶部圆角
    }

    private val dialog = BottomSheetDialog(ctx)

    private var bgLy: LinearLayout? = null
    private var titleTv: TextView? = null
    private var dateTv: TextView? = null
    private var datePicker: DateTimePicker? = null
    private var cancelTv: TextView? = null
    private var confirmTv: TextView? = null


    private var dateTimeFmt : String? = null

    init {
        dialog.setContentView(R.layout.dlg_time_picker)
        val bottomSheet = dialog.delegate.findViewById<FrameLayout>(R.id.design_bottom_sheet)
        bottomSheet?.setBackgroundColor(Color.TRANSPARENT)

        bgLy = dialog.findViewById(R.id.bgLy)
        titleTv = dialog.findViewById(R.id.titleTv)
        datePicker = dialog.findViewById(R.id.dateTimePicker)
        dateTv = dialog.findViewById(R.id.dateTv)
        cancelTv = dialog.findViewById(R.id.cancelTv)
        confirmTv = dialog.findViewById(R.id.confirmTv)

        initViews()
    }


    private fun initViews() {
        setBackground(CARD)
        setDataTimeFormat(null)

        datePicker?.setTextSize(15)

        datePicker?.setListener { time ->
            updateTime(time)
        }
    }

    private fun updateTime(time : Long){
        dateTimeFmt?.let {
            dateTv?.text = DateFormat.format(it, time)
        }
    }

    fun setBackground(background: Int): DatePickerDialog {
        val parmas = FrameLayout.LayoutParams(bgLy!!.layoutParams)
        when (background) {
            CARD -> {
                val margin = DispUtils.dp2px(ctx,12f)
                parmas.setMargins(margin, margin, margin, margin)
                bgLy?.layoutParams = parmas

                val gd = GradientDrawable()
                gd.setColor(Color.WHITE)
                gd.cornerRadius = DispUtils.dp2px(ctx,5f).toFloat()
                bgLy?.background = gd
            }
            CUBE -> {
                parmas.setMargins(0, 0, 0, 0)
                bgLy?.layoutParams = parmas
                bgLy?.setBackgroundColor(0xFFFFFF)
            }
            STACK -> {
                parmas.setMargins(0, 0, 0, 0)
                bgLy?.layoutParams = parmas

                val gd = GradientDrawable()
                gd.setColor(Color.WHITE)
                val radius = DispUtils.dp2px(ctx,15f).toFloat()
                gd.cornerRadii = floatArrayOf(radius,radius,radius,radius,0f,0f,0f,0f)
                bgLy?.background = gd
            }
            else -> {
                parmas.setMargins(0, 0, 0, 0)
                bgLy?.layoutParams = parmas
                bgLy?.setBackgroundResource(background)
            }
        }

        return this
    }

    /**
     * 设置主题颜色
     */
    fun setThemeColor(@ColorInt themeColor: Int): DatePickerDialog {
        if (themeColor != 0) {
            datePicker?.setThemeColor(themeColor)
            confirmTv?.setTextColor(themeColor)
        }
        return this
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String?): DatePickerDialog {
        if (title.isNullOrEmpty()){
            titleTv?.visibility = View.GONE
        }else{
            titleTv?.text = title
            titleTv?.visibility = View.VISIBLE
        }
        return this
    }

    /**
     * "yyyy年MM月dd日 c"
     */
    fun setDataTimeFormat(format:String?): DatePickerDialog {
        dateTimeFmt = format
        if (format.isNullOrEmpty()){
            dateTv?.visibility = View.GONE
        }else{
            dateTv?.visibility = View.VISIBLE
            updateTime(datePicker!!.getTime())
        }

        return this
    }

    /**
     * 设置显示值
     * 后面改成format形式(参考DateFormat.format)
     */
    fun setDisplayType(vararg types: Int): DatePickerDialog {
//        datePicker?.setDisplayType(intArrayOf(
//            DateTimeConfig.YEAR,
//            DateTimeConfig.MONTH,
//            DateTimeConfig.DAY,
//            DateTimeConfig.HOUR,
//            DateTimeConfig.MIN,
//            DateTimeConfig.SECOND
//        ))
        datePicker?.setDisplayType(types)
        return this
    }

    /**
     * 设置标签文字
     */
    fun setLabelText(year:String, month:String, day:String,
                     hour:String, min:String, sec:String): DatePickerDialog {
        datePicker?.setLabelText(year,month,day,hour,min,sec)
        return this
    }

    /**
     * 设置是否循环滚动
     */
    fun setWheel(enable: Boolean): DatePickerDialog {
        if (enable)
            datePicker?.setWheel(YEAR, MONTH, DAY, HOUR, MIN, SECOND)
        else
            datePicker?.setWheel()

        return this
    }

    /**
     * 设置范围最小值
     */
    fun setMinTime(time: Long): DatePickerDialog {
        datePicker?.setMinTime(time)
        return this
    }

    /**
     * 设置范围最大值
     */
    fun setMaxTime(time: Long): DatePickerDialog {
        datePicker?.setMaxTime(time)
        return this
    }

    /**
     * 设置默认时间
     */
    fun setDefaultTime(time: Long): DatePickerDialog {
        datePicker?.setDefaultTime(time)
        updateTime(time)
        return this
    }

    /**
     * 绑定选择监听
     */
    fun setOnConfirmListener(text: String, listener: ((Long) -> Unit)? = null): DatePickerDialog {
        confirmTv?.text = text
        confirmTv?.setOnClickListener{
            dialog.dismiss()
            listener?.invoke(datePicker!!.getTime())
        }

        return this
    }

    /**
     * 绑定取消监听
     */
    fun setOnCancelListener(text: String, listener: (() -> Unit)? = null): DatePickerDialog {
        cancelTv?.text = text
        cancelTv?.setOnClickListener{
            dialog.dismiss()
            listener?.invoke()
        }

        return this
    }


    fun show(): DatePickerDialog {
        dialog.show()
        return this
    }
}